package com.googlecode.tda367.denty;

import java.util.List;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.googlecode.tda367.denty.core.DentyModel;
import com.googlecode.tda367.denty.core.dynamicbody.DynamicBody;
import com.googlecode.tda367.denty.core.level.Level;
import com.googlecode.tda367.denty.ctrl.DBController;
import com.googlecode.tda367.denty.ctrl.EnemyController;
import com.googlecode.tda367.denty.ctrl.InputController;
import com.googlecode.tda367.denty.view.DentyView;

public class DentyGame extends BasicGame{

	private DentyModel model;
	private DentyView view;
	private InputController inputController;

	public DentyGame(String title) {
		super(title);
		model = DentyModel.getInstance();
		view = DentyView.getInstance();
		inputController = new InputController(model);
	}

	@Override
	public void render(GameContainer container, Graphics g) throws SlickException {
		this.view.render(container, g);
		
	}

	@Override
	public void init(GameContainer container) throws SlickException {
		// Loads physics engine, level and so on
		this.model.init(container);
		// Loads graphics
		this.view.init();
		
	}

	@Override
	public void update(GameContainer container, int delta) throws SlickException {
		model.update(container, delta);
		inputController.update(container.getInput());
		if(inputController.hasExited())
			container.exit();
		
		List<DynamicBody> bodyList = model.getDynamicBodies();
		Level level = model.getLevel();
		EnemyController.update(bodyList, level.getDenty().getPosition());
		DBController.update(bodyList, level.getWidth(), level.getHeight());
	}

}
